﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Qiang.Controls
{
    [Serializable]
    public class MapLayerCollection : LayerEvent,IList<IMapLayer>
    {

        #region 初始化

        /// <summary>
        /// 
        /// </summary>
        public MapLayerCollection()
            : base()
        {
            this._layers = new List<IMapLayer>();
        }

        #endregion

        #region 字段

        private List<IMapLayer> _layers;

        #endregion

        #region 属性

        /// <summary>
        /// 图层集合
        /// </summary>
        public List<IMapLayer> Layers
        {
            get { return _layers; }
            set
            {
                _layers = value; 

            }
        }

        /// <summary>
        /// 图层数量
        /// </summary>
        public int Count
        {
            get
            {
                return _layers.Count;
            }          
        }

        /// <summary>
        /// 是否是只读
        /// </summary>
        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public IMapLayer this[int index]
        {
            get
            {
                return _layers[index];
            }
            set
            {
                ExcludeLayer(_layers[index]);
                _layers[index] = value;
                IncludeLayer(value);

            }
        }


        #endregion

        #region 添加图层

        /// <summary>
        /// 添加图层
        /// </summary>
        /// <param name="mapLayer"></param>
        public void Add(IMapLayer layer)
        {
            _layers.Add(layer);
            IncludeLayer(layer);            
        }

        /// <summary>
        /// 包含图层
        /// </summary>
        /// <param name="layer"></param>
        void IncludeLayer(IMapLayer layer)
        {
            layer.DatasetContentChanged += new EventHandler(layerContentChanged);
            this.OnLayerAdded(this, new LayerEventArgs(layer));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void layerContentChanged(object sender, EventArgs e)
        {
            OnDatasetContentChanged(sender, e);
        }


        /// <summary>
        /// 触发图层添加事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void OnLayerAdded(object sender, LayerEventArgs e)
        {
            base.OnLayerAdded(sender, e);
        }

        /// <summary>
        /// 触发图层内容改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void OnDatasetContentChanged(object sender, EventArgs e)
        {
            base.OnDatasetContentChanged(sender, e);
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public IMapLayer GetLayer(int index)
        {
            return this._layers[index];
        }


        #region 删除图层

        /// <summary>
        /// 删除图层
        /// </summary>
        /// <param name="mapLayer"></param>
        public bool Remove(IMapLayer layer)
        {
            if (_layers.Remove(layer))
            {
                ExcludeLayer(layer);
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除Index位置的图层
        /// </summary>
        /// <param name="index"></param>
        public void RemoveAt(int index)
        {
            IMapLayer layer = _layers[index];
            Remove(layer);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="layer"></param>
        void ExcludeLayer(IMapLayer layer)
        {
            layer.DatasetContentChanged -= new EventHandler(layerContentChanged);
            this.OnLayerRemoved(this, new LayerEventArgs(layer));
        }


        /// <summary>
        /// 触发图层删除事个
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void OnLayerRemoved(object sender, LayerEventArgs e)
        {
            base.OnLayerRemoved(sender, e);
        }

        /// <summary>
        /// 删除所有图层
        /// </summary>
        public void RemoveAllLayers()
        {
            this._layers.Clear();
        }

        #endregion


        #region 方法

        /// <summary>
        /// 清空
        /// </summary>
        public void Clear()
        {
 
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="layer"></param>
        public bool Contains(IMapLayer layer)
        {
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        /// <param name="layer"></param>
        public void Insert(int index, IMapLayer layer)
        {
            _layers.Insert(index, layer);
            IncludeLayer(layer);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="layer"></param>
        /// <returns></returns>
        public int IndexOf(IMapLayer layer)
        {
            return _layers.IndexOf(layer);
        }


        public void CopyTo(IMapLayer[] layers,int index)
        {
            _layers.CopyTo(layers, index);
        }


        public IEnumerator<IMapLayer> GetEnumerator()
        {
           return  _layers.GetEnumerator();
        }

        
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _layers.GetEnumerator();
        }


        #endregion

    }
}
